#pragma once
#include "stdafx.h"
#include "units.h"
#include "common.h"
#include "hydraulic_item.h"
#include "automatic_item.h"


class Common;
class hydraulicItem;
class automaticItem;
class virtualConnector;
//class Title;
class hydConPoint;
class hydConnector;
class automaticItem;
class autConnector;

class SceneView : public QGraphicsScene
{
	Q_OBJECT

public:
	SceneView(Common* _common);
	~SceneView();

	Common* common;
	hydraulicItem* addHydraulicItem(const QPointF &_pos, const int &_type, const QString &_name = "", const bool &_visibleTitle = true);
	automaticItem* addAutomaticItem(const QPointF &_pos, const int &_type, const QString &_parent_name = "", const QString &_name = "", const bool &_visibleTitle = true);
	hydConnector* addHydConnector(hydConPoint* _source, hydConPoint* _dest, const QString &_coordinates = "", const QPointF &_spot_pos = QPointF());
	autConnector* addAutConnector(autConPoint* _source, autConPoint* _dest, const QString &_coordinates = "", const QPointF &_spot_pos = QPointF());
	void addVirtualHydConnector(hydConPoint* _point);
	void addVirtualAutConnector(autConPoint* _point);
	hydraulicItem* findHydItemByName(const QString &_name);
	automaticItem* findAutItemByName(const QString &_name);
	hydConnector* findHydConnectorByName(const QString &_name);
	Sensor* findSensorByName(const QString &_item_name, const QString &_sensor_name);
	int getIndexInList(hydraulicItem *item);
	void eraseHydItemFromList(hydraulicItem* item);
	void eraseAutItemFromList(automaticItem* item);
	void eraseHydConnectorFromList(hydConnector* connector);
	void eraseAutConnectorFromList(autConnector* connector);
	QStringList getItemsName();
	QStringList getAutItemsNameWithParameters();
	QStringList getSensorsName(const QString &item_name);
	vector<hydraulicItem*> getHydraulicItemList(){ return hydraulic_item_list; };
	vector<automaticItem*> getAutomaticItemList(){ return automatic_item_list; };
	vector<hydConnector*> getHydConnectorList(){ return hyd_connector_list; };
	vector<autConnector*> getAutConnectorList(){ return aut_connector_list; };
	bool isUniqueName(QGraphicsObject* item, const QString &_name) const;
private:
	bool redraw_virtual_connector;
	int dropped_item;
	hydraulicItem* virtual_item;
	automaticItem *virtual_automatic_item;
	virtualConnector* virtual_connector;
	void cutConnector(int index, hydConPoint* point);
	void DeleteSelectedItem();
	bool findInnerNodeNearMouse(const QPointF &_pos);
	void redrawVirtualHydConnector(const QPointF &pos);
	void redrawVirtualAutConnector(const QPointF &pos);
	void releaseVirtualHydConnector(const QPointF &pos);
	void releaseVirtualAutConnector(const QPointF &pos);

	vector<automaticItem*> automatic_item_list;
	vector<hydraulicItem*> hydraulic_item_list;
	vector<hydConnector*> hyd_connector_list;
	vector<autConnector*> aut_connector_list;
public slots:
	void indexDroppedItemChanged(int index);
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
	void keyPressEvent(QKeyEvent *event);
	void keyReleaseEvent(QKeyEvent *event);
	void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
	void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
	void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
	void dropEvent(QGraphicsSceneDragDropEvent *event);
};
